<template>
  <a-card :loading="cardLoading" :bordered="false" style="height: 100%;">
    <a-spin :spinning="loading" >
     <a-input-search @search="handleSearch" style="width:100%;margin-top: 10px" placeholder="输入表名称查询..." enterButton />
      <a-divider style="margin: 8px 0px 0px 0px"/>
      <div style="height:280px;overflow-y:auto">
        <a-tree
          :showLine= "true"
          checkStrictly
          :expandedKeys.sync="expandedKeys"
          :selectedKeys="selectedKeys"
          :dropdownStyle="{maxHeight:'100px',overflow:'auto'}"
          :treeData="treeDataSource"
          :load-data="onLoadData"
          @select="handleTreeSelect"
        />
      </div>

    </a-spin>
  </a-card>
</template>

<script>
  import { queryDepartTreeList, searchByKeywords,queryTableTreeList } from '@/api/api'
  const treeData = [
    {
      title: '客户信息',
      key: 'KHXX',
      istable: false,
      children: [
        { title: '个人客户信息表', key: 'LLBB_PRIVATE_CUSTOMER',children:[],istable:true },
        { title: '对公客户信息表', key: 'LLBB_CORPORATE_CUSTOMER',children:[],istable:true }
      ],
    },
    {
      title: '机构信息',
      key: 'JGXX',
      children: [
        { title: '金融机构法人信息表', key: 'LLBB_FINANCE_LEGAL_PERSON',children:[],istable:true },
        { title: '金融机构分支信息表', key: 'LLBB_FINANCE_BRANCH',children:[],istable:true }
      ],
    },
    {
      title: '贷款信息',
      key: 'DKXX',
      children: [
        { title: '个人贷款基础信息表', key: 'LLBB_PERSONAL_LOAN_BASIS',children:[],istable:true },
        { title: '个人贷款余额信息表', key: 'LLBB_PERSONAL_LOAN_BALANCE',children:[],istable:true },
        { title: '个人贷款放款信息表', key: 'LLBB_PERSONAL_LOAN',children:[],istable:true },

        { title: '非同业单位贷款基础信息表', key: 'LLBB_UNIT_LOAN_BASIS',children:[],istable:true },
        { title: '非同业单位贷款余额信息表', key: 'LLBB_UNIT_LOAN_BALANCE',children:[],istable:true },
        { title: '非同业单位贷款放款信息表', key: 'LLBB_UNIT_LOAN',children:[],istable:true },

        { title: '透支业务余额信息表', key: 'LLBB_O_BUSINESS_BALANCE',children:[],istable:true },
        { title: '透支业务交易流水表', key: 'LLBB_O_BUSINESS_T_FLOW',children:[],istable:true },
        { title: '透支业务账单信息表', key: 'LLBB_OVERDRAFT_BUSINESS_BILL',children:[],istable:true },
        { title: '透支业务分期信息表', key: 'LLBB_OVERDRAFT_BUSINESS_STAGES',children:[],istable:true },

        { title: '委托贷款基础信息表', key: 'LLBB_ENTRUSTED_LOAN_BASIS',children:[],istable:true },
        { title: '委托贷款余额信息表', key: 'LLBB_ENTRUSTED_LOAN_BALANCE',children:[],istable:true },
        { title: '委托贷款放款信息表', key: 'LLBB_ENTRUSTED_LOAN',children:[],istable:true },
      ],
    },
    {
      title: '存款信息',
      key: 'CKXX',
      children: [
        { title: '个人存款基础信息表', key: 'LLBB_PERSONAL_DEPOSIT_BASIS',children:[],istable:true },
        { title: '个人存款余额信息表', key: 'LLBB_PERSONAL_DEPOSIT_BALANCE',children:[],istable:true },
        { title: '个人存款发生额信息表', key: 'LLBB_PERSONAL_DEPOSIT',children:[],istable:true },
        { title: '非同业单位存款基础信息表', key: 'LLBB_UNIT_DEPOSIT_BASIS',children:[],istable:true },
        { title: '非同业单位存款余额信息表', key: 'LLBB_UNIT_DEPOSIT_BALANCE',children:[],istable:true },
        { title: '非同业单位存款发生额信息表', key: 'LLBB_UNIT_DEPOSIT',children:[],istable:true },
      ],
    },
    {
      title: '票据信息',
      key: 'PJXX',
      children: [
        { title: '票据贴现及转贴现基础信息表', key: 'LLBB_DISCOUNT_REDISCOUNT_BASIS',children:[],istable:true },
        { title: '票据贴现及转贴现余额信息表', key: 'LLBB_D_REDISCOUNT_BALANCE',children:[],istable:true },
        { title: '票据贴现及转贴现发生额信息表', key: 'LLBB_DISCOUNT_REDISCOUNT',children:[],istable:true },
      ],
    },
    {
      title: '同业信息',
      key: 'TYXX',
      children: [
        { title: '同业存款基础信息表', key: 'LLBB_SAME_UNIT_DEPOSIT_BASIS',children:[],istable:true },
        { title: '同业存款余额信息表', key: 'LLBB_SAME_UNIT_DEPOSIT_BALANCE',children:[],istable:true },
        { title: '同业存款发生额信息表', key: 'LLBB_SAME_UNIT_DEPOSIT',children:[],istable:true },

        { title: '同业借贷基础信息表', key: 'LLBB_SAME_UNIT_LOAN_BASIS',children:[],istable:true },
        { title: '同业借贷余额信息表', key: 'LLBB_SAME_UNIT_LOAN_BALANCE',children:[],istable:true },
        { title: '同业借贷发生额信息表', key: 'LLBB_SAME_UNIT_LOAN',children:[],istable:true },

        { title: '买入返售及卖出回购基础信息表', key: 'LLBB_BUY_SELL_BACK_BASIS',children:[],istable:true },
        { title: '买入返售及卖出回购余额信息表', key: 'LLBB_BUY_SELL_BACK_BALANCE',children:[],istable:true },
        { title: '买入返售及卖出回购发生额信息表', key: 'LLBB_BUY_SELL_BACK',children:[],istable:true },

      ],
    },
    {
      title: 'FTP信息',
      key: 'FTPXX',
      children: [
        { title: 'FTP定价变动明细信息表', key: 'LLBB_FTP_CHANGE_DETAILS',children:[],istable:true },

      ],
    },
    {
      title: '利差息差',
      key: 'LCXC',
      children: [
        { title: '人民币利差息差统计表', key: 'LLBB_RMB_INTEREST_SPREAD',children:[],istable:true },
      ],
    },

  ];
  export default {
    name: 'TableListLeft',
    props: ['value'],
    data() {
      return {
        tableName: '',
        columnName: '',
        title: '',
        cardLoading: true,
        loading: false,
        treeData,
        treeDataSource: [],
        selectedKeys: [],
        expandedKeys: []
      }
    },
    created() {
    this.queryTreeData()
    },
    methods: {
      onLoadData(treeNode) {
        return new Promise(resolve => {
          /*if (treeNode.dataRef.children) {
            resolve();
            return;
          }*/

          if (treeNode.dataRef.children.length == 0) {
            queryTableTreeList({
              tableName: treeNode.dataRef.key ? treeNode.dataRef.key : undefined
            }).then(res => {
              if (res.success) {
                treeNode.dataRef.children = res.result
                resolve();
              } else {
                this.$message.warn(res.message)
                console.error('表结构查询失败:', res)
                resolve();
              }
            }).finally(() => {
              resolve();
            })
          }
          resolve();
        })

         /* if (treeNode.dataRef.children) {
            resolve();
            return;
          }
          setTimeout(() => {
            treeNode.dataRef.children = [
              { title: 'Child Node', key: `${treeNode.eventKey}-0` },
              { title: 'Child Node', key: `${treeNode.eventKey}-1` },
            ];
            this.treeData = [...this.treeData];
            resolve();
          }, 1000);*/

      },

      queryTreeData(keyword) {
        this.treeDataSource = treeData
        this.loading = false
        this.cardLoading = false
      /* this.commonRequestThen(keyword,queryTableTreeList({
          tableName: treeNode.dataRef.key ? keyword : undefined
        }))*/
      },

      handleSearch(value) {
        if (value) {
          queryTableTreeList({
            tableNameCN: value ? value : undefined
          }).then(res => {
            if (res.success) {
              this.treeDataSource = res.result
            } else {
              this.$message.warn(res.message)
              console.error('表结构查询失败:', res)
            }
          }).finally(() => {
          })
        } else {
          this.queryTreeData()
        }
      },

      handleTreeSelect(selectedKeys, event) {
          this.selectedKeys = [selectedKeys[0]]
          this.tableName = event.node.dataRef.tableName
          this.columnName = event.node.dataRef.columnName
          this.title = event.node.dataRef.title
          this.$emit('tableValue', this.tableName, this.columnName,this.title)
         /* let orgCode = event.node.dataRef.orgCode
          this.emitInput(orgCode)*/

      },

      emitInput(orgCode) {
        this.$emit('input', orgCode)
      },

      commonRequestThen(treeNode,promise) {
        this.loading = true
        promise.then(res => {
          if (res.success) {
            treeNode.dataRef.children  = res.result
            // update-begin- --- author:wangshuai ------ date:20200102 ---- for:去除默认选中第一条数据、默认展开所有第一级
            // 默认选中第一条数据、默认展开所有第一级
            // if (res.result.length > 0) {
            //   this.expandedKeys = []
            //   res.result.forEach((item, index) => {
            //     if (index === 0) {
            //       this.selectedKeys = [item.id]
            //       this.emitInput(item.orgCode)
            //     }
            //     this.expandedKeys.push(item.id)
            //   })
            // }
           // update-end- --- author:wangshuai ------ date:20200102 ---- for:去除默认选中第一条数据、默认展开所有第一级
          } else {
            this.$message.warn(res.message)
            console.error('表结构查询失败:', res)
          }
        }).finally(() => {
          this.loading = false
          this.cardLoading = false
        })
      },

    }
  }
</script>

<style scoped>

</style>